package com.guigu.controller;

import com.guigu.dto.SportseventsDTO;
import com.guigu.dto.TemplateManagementDTO;
import com.guigu.dto.mealsDTO;
import com.guigu.dto.motion_summaryDTO;
import com.guigu.entity.HealthPlan;
import com.guigu.entity.meals;
import com.guigu.exception.SsmException;
import com.guigu.service.mealsService;
import com.guigu.service.motion_summaryService;
import com.guigu.vo.CodeGenerator;
import com.guigu.vo.Result;
import com.guigu.vo.SportseventsVO;
import com.guigu.vo.mealsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/meals")
public class mealsController {
    @Autowired
    private mealsService meservice;

    @Autowired
    private motion_summaryService summaryService;

    @PostMapping("/querym")
    public Result query(@RequestBody mealsDTO dto){
        try{
            List<mealsVO> list=this.meservice.query(dto);
            System.err.println(dto);
            if(!list.isEmpty()){
                return new Result<List<mealsVO>>("200", "true", "查询成功", list);
            }else if(dto.getName()!=null || dto.getTypename()!= null){
                return new Result<List<mealsVO>>("200", "true", "没有对应的数据", list);
            }else{
                return new Result<List<mealsVO>>("400", "false", "查询失败", null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new SsmException("异常");
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody meals ms){
        try{

            Integer id=this.meservice.maxid();

            // 如果id为null，将其默认设置为0
            if (id==null) {
                id = 0;
            }
            //生成根据id编码
            CodeGenerator generator = new CodeGenerator("SP");
            String newCode = generator.generateCode(id+1);
            System.out.println(newCode);
            ms.setCodes(newCode);
            System.err.println(ms);
            int i=this.meservice.add(ms);
            if(i==1){
                return new Result<>("200", "true", "添加成功", null);
            }else{
                return new Result<>("400", "false", "添加失败", null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw  new SsmException("异常");
        }
    }
    @PostMapping("/update")
    public Result update(@RequestBody meals ms){
        try{
            int id=this.meservice.update(ms);
            if(id==1){
                return new Result<>("200", "true", "修改成功", null);
            }else{
                return new Result<>("400", "false", "修改失败", null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw  new SsmException("异常");
        }
    }

    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable int id){
        try{
            int i= this.meservice.delete(id);
            if(i==1){
                return new Result<>("200", "true", "删除成功", null);
            }else {
                return new Result<>("400", "false", "删除失败", null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw  new SsmException("异常");
        }
    }
    @PostMapping("/save")
    public Result save(@RequestBody motion_summaryDTO motion_summary){

        this.summaryService.saveMotion_summary(motion_summary);
        return new Result<>("200", "true", "保存成功", "无");
    }

}
